import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { showNotify } from "vant";
import "vant/es/notify/style";
import storage from "@/utils/storerage";
const routes = [

  {
    path: '/',
    redirect: '/home/index'  // 重定向
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'index',
        component: () => import('../views/index/IndexView.vue')
      },
      {
        path: 'activity',
        component: () => import('../views/activity/ActivityClassification.vue'),
      },
      {
        path: 'shop',
        component: () => import('../views/shop/ShopView.vue'),
      },
      {
        path: 'me',
        component: () => import('../views/me/MeView.vue'),
        meta: {
          isAuth: true
        }
      },
      {
        path: 'huobao',
        component: () => import('../views/index/HuoBao.vue'),
      },
      {
        path: 'search',
        component: () => import('../views/index/SearchView.vue'),
      },
      {
        path: 'newproduct',
        component: () => import('../views/index/NewProduct.vue'),
      },
      {
        path: 'drinktype',
        component: () => import('../views/index/DrinkType.vue'),
      },
      {
        path: 'special',
        component: () => import('../views/index/SpecialView.vue'),
      },
      {
        path: 'forum',
        component: () => import('../views/index/ForumView.vue'),
      },
      {
        path: '/one',
        component: () => import('../views/shop/OneView.vue'),
      },
      {
        path: '/details',
        component: () => import('../views/shop/DetailsView.vue'),
      },
      {
        path: 'more',
        component: () => import('../views/index/MoreView.vue'),
      },
      {
        path: 'card-search',
        component: () => import('../views/index/CardSearch.vue'),
      },
      {
        path: 'guide',
        component: () => import('../views/index/GuideView.vue'),
      },
    ]
  },

  {
    path: "/login",
    component: () => import("../views/me/LoginView.vue"),
  },
  {
    path: "/zhuce",
    component: () => import("../views/me/ZhuceView.vue"),
  },

  {
    path: '/activity/premium-selection',
    component: () => import('../views/activity/PremiumSelection.vue'),
  },
  {
    path: '/activity/couple-special-session',
    component: () => import('../views/activity/CoupleSpecialSession.vue'),
  },
  {
    path: '/activity/classic-taste',
    component: () => import('../views/activity/ClassicTaste.vue'),
  },
  {
    path: '/activity/flash-sale',
    component: () => import('../views/activity/FlashSale.vue'),
  },
  {
    path: '/activity/activity-four-view',
    component: () => import('../views/activity/ActivityFourView.vue'),
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to.path, from.path)
  if (to.meta.isAuth && !localStorage.getItem("Authorization")) {
    showNotify({ type: "warning", message: "请先登录在查看！" });
    return next("/login");
  } else if (from.path !== "/home/me")
    localStorage.setItem(storage.KEY.CurrentPath, from.path);
  next();
});

export default router
